﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xiaowei.Diagnostics;

namespace Xiaowei.Players
{
    public class PlayerManager
    {
        private IPlayer currentPlayer;
        public IPlayer CurrentPlayer
        {
            get
            {
                return currentPlayer;
            } 
            set
            {
                if(currentPlayer!=value)
                {
                    Suspend();
                    currentPlayer = value;
                }
            }
        }
        bool isSuspend = true;
        public void Suspend()
        {
            if (!isSuspend)
            {
                Debug.WriteLine("PlayerManager, Suspend");
                currentPlayer?.Suspend();
                isSuspend = true;
            }
        }


        /// <summary>
        ///resume by xiaowei,player will do command list. 
        /// </summary>
        public void Resumed()
        {
            if (!isSuspend)
            {
                Debug.WriteLine("PlayerManager, Resumed -- no suspended");
                return;
            }
            isSuspend = false;
            currentPlayer?.Resumed();
            Debug.WriteLine("PlayerManager, Resumed");
        }
        /// <summary>
        /// resumed2 will be called if user click play button while player is suspend.
        /// </summary>
        public void Resumed2()
        {
            if (!isSuspend) return;
            isSuspend = false;
            currentPlayer?.Resumed2();
            Debug.WriteLine("PlayerManager, Resumed2");
        }

        private PlayerManager()
        {

        }
        private static Lazy<PlayerManager> lazyPlayerManager = new Lazy<PlayerManager>(new PlayerManager());
        public static PlayerManager Instance => lazyPlayerManager.Value;

    }
}
